#include<QCoreApplication>
#include<QList>
#include<QString>
#include<QDebug>
QString getLongestCommonPrefix(QList <QString> strs);
bool IsEnglish(QString &qstrSrc);
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<QString> list1,list2,list3;
    list1<<"ab"<<"abvx"<<"aaaaa";//CASE1
    list2<<"nih"<<"niwo"<<"sdd";//CASE2
    list3<<"我我";//ERROR CASE TEST
    qDebug() << getLongestCommonPrefix(list1);
    qDebug() << getLongestCommonPrefix(list2);
    qDebug() << getLongestCommonPrefix(list3);
    return a.exec();
}
QString getLongestCommonPrefix(QList <QString> strs)
{
    int minStrLength;
    //this part is to find the minimum of the length of string.
    for(int i=0;i<strs.length();i++){
        if(!IsEnglish(strs[i]))
        {
            QString s="NOT A ENGLISH CHARACTER!";
            return s;
        }
        int strLength=strs[i].length();
        if(i==0)
        {
            minStrLength=strLength;
        }
        else
        {
          if (strLength<minStrLength)
          {
               minStrLength=strLength;
          }
        }
    }
    int count,mincount;
    //the first two loops(i,j) is to compare all of the strings.
    //the final loops(k) is to compare the common characters literally, and count for output.
    for(int i=0;i<strs.length();i++)
    {
        for(int j=i+1;j<strs.length();j++)
        {
            count=0;
            for(int k=0;k<minStrLength;k++)
            {
                if (strs[i].at(k)==strs[j].at(k))
                {
                    count+=1;
                }
            }
        }
        if(i==0)
        {
            mincount=count;
        }
        else
        {
            if(count<mincount)
                mincount=count;
        }

    }
    QString s=strs[1].left(mincount);
    return s;
}
bool IsEnglish(QString &qstrSrc)
{
    QByteArray ba = qstrSrc.toLatin1();
    const char *s = ba.data();
    bool bret = true;
    while(*s)
    {
        if((*s>='A' && *s<='Z') || (*s>='a' && *s<='z'))
        {

        }
        else
        {
            bret = false;
            break;
        }
        s++;
    }
    return bret;
}
